package model.decoder;

import model.location.ReferencePoint;
import model.location.ReferencePointFactory;
import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;
import android.hardware.Camera.Size;
import android.util.Log;

public class ZBarDecoder extends Decoder<Symbol>
{
	private ImageScanner scanner;
	
	public ZBarDecoder(Size previewSize)
	{
		this._previewSize = previewSize;
		
		scanner = new ImageScanner();
		
        scanner.setConfig(0, Config.X_DENSITY, 3);
        scanner.setConfig(0, Config.Y_DENSITY, 3);
	}
	
	@Override
	protected Symbol[] doInBackground(byte[]... bytes) 
	{
		super.doInBackground(bytes);
		
		Image barcode = new Image(_previewSize.width, _previewSize.height, "GREY");
        barcode.setData(bytes[0]);

        int result = scanner.scanImage(barcode);
        Log.d("CameraPreviewCallback", String.format("result: %d", result));
        
        if (result >= 1) 
        {       	
        	SymbolSet set = scanner.getResults();
        	return set.toArray(new Symbol[set.size()]);
        }
        return null;
	}

	@Override
	protected String getDecodedData(Symbol result) 
	{
		return result.getData();
	}

	@Override
	protected ReferencePoint createReferencePoint(Symbol result)
	{
		return ReferencePointFactory.createReferencePoint(result);
	}		
}